﻿using System;
using Native_.DirectShow;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
namespace Capture.DirectShow
{
    /// <summary>
    /// DeviceInfo
    /// </summary>
    public struct DeviceInfo
    {
        public static readonly DeviceInfo Empty = new DeviceInfo();
        public string Moniker;

        public static DeviceInfo GetDefaultDevice()
        {
            var devices = GetDevices();
            return ((devices == null) || (devices.Length == 0) ? Empty : new DeviceInfo
            {
                Moniker = devices[0].MonikerString,
            });
        }

        public static FilterInfo[] GetDevices()
        {
            var filters = new List<FilterInfo>();
            var ms = new IMoniker[1];
            var deviceEnum = (Activator.CreateInstance(Type.GetTypeFromCLSID(Registry.SystemDeviceEnum)) as ICreateDevEnum);
            IEnumMoniker moniker;
            var g = Registry.VideoInputDevice;
            if (deviceEnum.CreateClassEnumerator(ref g, out moniker, 0) == 0)
                while (true)
                {
                    int r = moniker.Next(1, ms, IntPtr.Zero);
                    if ((r != 0) || (ms[0] == null))
                        break;
                    filters.Add(new FilterInfo(ms[0]));
                    Marshal.ReleaseComObject(ms[0]);
                    ms[0] = null;
                }
            return filters.ToArray();
        }
    }
}
